package com.gitee.ywj1352.bussiness.biz;

import com.gitee.ywj1352.bussiness.service.OrderClient;
import com.gitee.ywj1352.bussiness.service.UserClient;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BusinessBiz {

    @Autowired
    OrderClient orderClient;

    @Autowired
    UserClient userClient;

    @GlobalTransactional(rollbackFor = RuntimeException.class)
    public void createUserAndOrder(String errorcode){
        System.out.println("purchase begin ... xid: " + RootContext.getXID());
        Long userId = userClient.addUser("i am ywj");
        orderClient.addOrder(userId);
        if ("e".equals(errorcode)){
            System.out.println("抛出异常");
            throw  new RuntimeException();
        }
    }
}
